//
//  QJRegistVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/2/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJRegistVC.h"
#import "QJUserDelegateVC.h"
#import "YFWebViewController.h"
@interface QJRegistVC ()

@end

@implementation QJRegistVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"注册";
    [self setupRegistViewAction];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    //控件不被导航栏遮挡
    self.navigationController.navigationBar.translucent = NO;
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    //控件不被导航栏遮挡
//    self.navigationController.navigationBar.translucent = YES;
}
#pragma mark -- 界面布局
- (void)setupRegistViewAction{

    self.checkboxBtn.selected = YES;
}
#pragma mark -- 验证码按钮点击方法
- (IBAction)codeBtnClickAction:(UIButton *)sender {
    WEAKSELF
    if (self.phoneTF.text.length != 11) {
        [MBPHUD showAlertWithTitle:@"手机号码为11位"];
    }else if (self.phoneTF.text.length < 1) {
        [MBPHUD showAlertWithTitle:@"请输入您要注册的手机号!"];
    }else if (![self.secrect.text isEqualToString:self.secrectOk.text]){
        [MBPHUD showAlertWithTitle:@"您两次输入的密码不一致!"];
    }else{
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:self.phoneTF.text forKey:@"mobile"];
        [dict setObject:@"1" forKey:@"scene"];
        [dict setObject:@"mobile" forKey:@"type"];
        
        [WWNetRequest fetchVerificationCodeWithParameters:dict success:^(id response) {
            
            NSLog(@"发送验证码 response = %@", response);
            if (response) {
                [MBPHUD showAlertWithTitle:@"验证码已发送到您的手机!"];
                [weakSelf openCountdownWithBtn:sender];
            }
        } failure:^(NSDictionary *error) {
            NSLog(@"发送验证码 error = %@", error);
        }];
    }
}
// 开启倒计时效果
-(void)openCountdownWithBtn:(UIButton *)sender{
    
    __block NSInteger time = 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){
            //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮的样式
                [sender setTitle:@"重新获取" forState:UIControlStateNormal];
                [sender setTitleColor:HexRGB(0x808080) forState:UIControlStateNormal];
                sender.userInteractionEnabled = YES;
            });
        }else{
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮显示读秒效果
                [sender setTitle:[NSString stringWithFormat:@"重新获取%d", seconds] forState:UIControlStateNormal];
                [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                sender.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}

#pragma mark -- 注册按钮点击方法
- (IBAction)registBtnClickAction:(UIButton *)sender {
    if (self.phoneTF.text.length < 1 || self.secrect.text.length < 1 || self.secrectOk.text.length < 1 || self.codeTF.text.length < 1 ) {
        [MBPHUD showAlertWithTitle:@"您填写的信息不完整!"];
    }else if (![self.secrect.text isEqualToString:self.secrectOk.text]){
        [MBPHUD showAlertWithTitle:@"您填写的密码不一致!"];
    }else if (!self.checkboxBtn.selected){
        [MBPHUD showAlertWithTitle:@"您还没有同意《注册协议》!"];
    }else if (self.phoneTF.text.length != 11) {
        [MBPHUD showAlertWithTitle:@"您输入的手机号格式错误!"];
    }else if (self.secrect.text.length < 6){
        [MBPHUD showAlertWithTitle:@"您输入的密码不足6位!"];
        return;
    }else if (self.secrect.text.length > 16){
        [MBPHUD showAlertWithTitle:@"您输入的密码超过16位!"];
        return;
    }
    else{
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:self.phoneTF.text forKey:@"username"];
        [dict setObject:self.secrect.text forKey:@"password"];
        [dict setObject:self.secrectOk.text forKey:@"password2"];
        [dict setObject:self.codeTF.text forKey:@"code"];
        [dict setObject:@"1" forKey:@"scene"]; // 验证码类型
        /*
         1注册 2找回密码 3客户下单 4客户支付 5商家发货 6身份验证 7购买虚拟商品通知）
         */
        [dict setObject:self.invitePhoneTF.text forKey:@"invite"];
    
//    NSLog(@"dict = %@", dict);
        [WWNetRequest fetchRegisterWithParameters:dict success:^(id response) {
            if (response) {
                [MBPHUD showAlertWithTitle:@"恭喜您已注册成功!"];
                UserInfoModel *model = [[UserInfoModel alloc]initWithDictionary:response error:nil];
                [QJUserInfo saveCurrentUser:model];
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
            
        } failure:^(NSDictionary *error) {
            NSLog(@"error = %@", error);
        }];
        
    }
}

- (IBAction)checkboxBtnClickAction:(UIButton *)sender {
    self.checkboxBtn.selected = !self.checkboxBtn.selected;
}
- (IBAction)userDelegateBtnClickAction:(UIButton *)sender {
    YFWebViewController *VC = [[YFWebViewController alloc]init];
    VC.urlString = @"http://www.quanjieshop.com/index.php/mobile/Article/detail/article_id/1415.html?app=1";
    KPushViewControllerAction(VC);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
